package com.xiaoc.bulldozer.facade.impl;

import com.xc.framework.common.exception.BusinessException;
import com.xiaoc.bulldozer.common.ConfigKeys;
import com.xiaoc.bulldozer.common.Constant;
import com.xiaoc.bulldozer.common.ResponseCode;
import com.xiaoc.bulldozer.common.dto.AwardDto;
import com.xiaoc.bulldozer.common.dto.AwardPartnerDto;
import com.xiaoc.bulldozer.common.dto.AwardPartnerQueryDto;
import com.xiaoc.bulldozer.common.model.Award;
import com.xiaoc.bulldozer.common.model.AwardPartner;
import com.xiaoc.bulldozer.common.model.WeixinUser;
import com.xiaoc.bulldozer.common.service.AccountService;
import com.xiaoc.bulldozer.common.service.AwardPartnerService;
import com.xiaoc.bulldozer.common.service.AwardService;
import com.xiaoc.bulldozer.common.service.WeixinUserService;
import com.xiaoc.bulldozer.common.service.cache.CommonConfigCacheService;
import com.xiaoc.bulldozer.facade.AwardPartnerFacade;
import com.xiaoc.bulldozer.facade.WeixinUserFacade;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

/**
 * describe:
 *
 * @author lihuancheng
 * @date 2018/8/30
 */
@Service("awardPartnerFacade")
public class AwardPartnerFacadeImpl implements AwardPartnerFacade {


    @Resource
    WeixinUserService weixinUserService;

    @Resource(name = "awardPartnerService")
    private AwardPartnerService awardPartnerService;

    @Resource(name = "awardService")
    private AwardService awardService;

    @Resource(name = "accountService")
    private AccountService accountService;

    @Resource
    private WeixinUserFacade weixinUserFacade;

    @Resource(name = "commonConfigService")
    private CommonConfigCacheService commonConfigService;

    private static final Logger logger = LoggerFactory.getLogger(AwardPartnerFacadeImpl.class);

    @Override
    public void updatePaternRegistStatusByUserIdAndPartnerUserId(Integer userId, Integer partnerUserId) {

        AwardPartner awardPartner = awardPartnerService.getAwardPartnerByUserIdAndPartnerUserId(userId, partnerUserId);

        if (awardPartner != null) {
            awardPartner.setPaternRegistStatus(Constant.ENABLE);
            awardPartner.setUpdateTime(System.currentTimeMillis());
            awardPartnerService.update(awardPartner);
        }

    }

    @Override
    public List<AwardPartnerDto> getAllPartnerListByUserId(Integer userId) {

        return awardPartnerService.getAllPartnerListByUserId(userId);
    }

    @Override
    public Integer getAllPartnerListCountByUserId(Integer userId) {

        return awardPartnerService.getAllPartnerListCountByUserId(userId);
    }

    @Override
    public int getRegistSuccessCountByUserIdAndRoundCount(Integer userId, Integer roundCount) {
        if (roundCount == null || roundCount.intValue() < 1) {
            return 0;
        }
        List<Award> awardList = awardService.getAwardByUserIdAndRoundCount(userId, roundCount);
        if (CollectionUtils.isEmpty(awardList)) {
            return 0;
        }
        Set<Integer> awardIds = new TreeSet<>();
        for (Award award : awardList) {
            awardIds.add(award.getId());
        }
        return awardPartnerService.getRegistSuccessCountByUserIdAndAwardIds(userId, awardIds);
    }


    @Override
    public List<AwardPartnerDto>   fillAwardPartnerDtoList(AwardPartnerQueryDto awardPartnerQueryDto) {

        List<AwardPartnerDto> awardPartnerDtoList = awardPartnerService.getAllPartnerListByUserIdAndPage(awardPartnerQueryDto);
        return  fillAwardPartnerDtoListByList(awardPartnerDtoList);
    }

    @Override
    public List<AwardPartnerDto> fillAwardPartnerDtoListByList(List<AwardPartnerDto> awardPartnerDtoList) {

        for(AwardPartnerDto awardPartnerDto : awardPartnerDtoList) {

            if(StringUtils.isEmpty(awardPartnerDto.getAvatarUrl())){
                awardPartnerDto.setAvatarUrl(commonConfigService.getValue(ConfigKeys.DEFAULT_WX_HEADER_URL));
            }
            if(StringUtils.isEmpty(awardPartnerDto.getNickName())){
                awardPartnerDto.setNickName("微信用户");
            }
        }
        return awardPartnerDtoList;
    }

    @Override
    public AwardPartner getRecentAwardPartnerByPartnerUserId(Integer partnerUserId) {
        return awardPartnerService.getRecentAwardPartnerByPartnerUserId(partnerUserId);
    }

    /**
     * 获取亲友团信息
     * @param userId 红包开启人ID
     * @param awardId 红包  ID
     * @param param 参数容器
     */
    @Override
    public void getParentGather(Integer userId, Integer awardId, Map params) {

        WeixinUser weixinUser = weixinUserService.getWeixinUserById(userId);

        if (weixinUser == null ) {
            logger.info("获取亲友团信息失败");
            throw new BusinessException(ResponseCode.BUSINESS_ERROR.getCode(), ResponseCode.BUSINESS_ERROR.getMsg());
        }

        //我的亲友团
        List<AwardPartnerDto> myPartnerDtoList = awardPartnerService.getAllPartnerListByUserId(userId);

        if (CollectionUtils.isNotEmpty(myPartnerDtoList)) {
            //小程序最多显示5条亲友团
            int listSize = myPartnerDtoList.size() > 5 ? 5 : myPartnerDtoList.size();
            myPartnerDtoList = myPartnerDtoList.subList(0, listSize);
            myPartnerDtoList = this.fillAwardPartnerDtoListByList(myPartnerDtoList);
        }

        params.put("myPartnerList", myPartnerDtoList);

        List<AwardDto> recentAwardList = awardService.getRecentAwardList(Integer.parseInt(commonConfigService.getValue(ConfigKeys.RECENT_AWARD_LIST_SIZE)));
        params.put("recentAwardList", recentAwardList);

        List<AwardPartnerDto> myAwardPartnerDtoList = awardPartnerService.getAwardPartnerList(awardId);

        params.put("myAwardPartnerDtoList", myAwardPartnerDtoList);
        params.put("myAwardPartnerCount", myAwardPartnerDtoList.size());

        params.put("photoUrl", weixinUser.getAvatarUrl());
    }
}
